package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.BeanUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.dao.StoreOutRecordProductMapper;
import com.zhijian.warehouse.manage.entity.po.StoreOutRecordProduct;
import com.zhijian.warehouse.manage.entity.vo.request.StoreOutRecordProductSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreOutRecordProductSearchVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreOutRecordProductVo;
import com.zhijian.warehouse.manage.service.StoreOutRecordProductService;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 出库产品明细表业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-10-27 13:45:55
 */
@Slf4j
@Service
public class StoreOutRecordProductServiceImpl implements StoreOutRecordProductService {

    @Autowired
    private StoreOutRecordProductMapper mapper;

    @Override
    public StoreOutRecordProduct selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public StoreOutRecordProductVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(StoreOutRecordProduct storeOutRecordProduct) {
        Date now = new Date();
        storeOutRecordProduct.setCreateTime(now);
        storeOutRecordProduct.setUpdateTime(now);
        return mapper.insert(storeOutRecordProduct) > 0 ? storeOutRecordProduct.getId() : 0L;
    }

    @Override
    public Long insertSelective(StoreOutRecordProduct storeOutRecordProduct) {
        Date now = new Date();
        storeOutRecordProduct.setCreateTime(now);
        storeOutRecordProduct.setUpdateTime(now);
        return mapper.insertSelective(storeOutRecordProduct) > 0 ? storeOutRecordProduct.getId() : 0L;
    }

    @Override
    public boolean update(StoreOutRecordProduct storeOutRecordProduct) {
        Date now = new Date();
        storeOutRecordProduct.setUpdateTime(now);
        return mapper.updateByPrimaryKey(storeOutRecordProduct) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(StoreOutRecordProduct record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(StoreOutRecordProductSaveOrUpdateVo saveOrUpdateVo) {
        StoreOutRecordProduct storeOutRecordProduct = new StoreOutRecordProduct();
        BeanUtils.copyProperties(saveOrUpdateVo, storeOutRecordProduct);
        return this.insert(storeOutRecordProduct) != 0;
    }

    @Override
    public boolean modify(StoreOutRecordProductSaveOrUpdateVo saveOrUpdateVo) {
        StoreOutRecordProduct old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        StoreOutRecordProduct storeOutRecordProduct = new StoreOutRecordProduct();
        BeanUtils.copyProperties(saveOrUpdateVo, storeOutRecordProduct);
        return this.update(storeOutRecordProduct);
    }

    @Override
    public StoreOutRecordProductVo selectOne(StoreOutRecordProductSearchVo storeOutRecordProductSearchVo) {
        return mapper.selectOne(storeOutRecordProductSearchVo);
    }

    @Override
    public Integer count(StoreOutRecordProductSearchVo storeOutRecordProductSearchVo) {
        return mapper.count(storeOutRecordProductSearchVo);
    }

    @Override
    public List<StoreOutRecordProductVo> selectList(StoreOutRecordProductSearchVo storeOutRecordProductSearchVo) {
        return mapper.selectList(storeOutRecordProductSearchVo);
    }

    @Override
    public Page<StoreOutRecordProductVo> selectPage(StoreOutRecordProductSearchVo storeOutRecordProductSearchVo) {
        AssertCommonUtil.check(Objects.isNull(storeOutRecordProductSearchVo.getPageNum()) || Objects.isNull(storeOutRecordProductSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(storeOutRecordProductSearchVo);
        //分页
        Page<StoreOutRecordProductVo> page = new Page<>(true, count, storeOutRecordProductSearchVo.getPageNum(), storeOutRecordProductSearchVo.getPageSize());
        storeOutRecordProductSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<StoreOutRecordProductVo> responseVos = this.selectList(storeOutRecordProductSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<StoreOutRecordProduct> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(StoreOutRecordProductSearchVo storeOutRecordProductSearchVo) {
        if (BeanUtil.isEmpty(storeOutRecordProductSearchVo.getStoreOutRecordId()))
            return;
        List<StoreOutRecordProductVo> storeOutRecordProductVos = this.selectList(storeOutRecordProductSearchVo);
        if (CollectionUtil.isEmpty(storeOutRecordProductVos)) {
            return;
        }
        List<Long> collect = storeOutRecordProductVos.stream().map(StoreOutRecordProductVo::getId).collect(Collectors.toList());
        this.deleteByIdList(collect);
    }

}